using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Lists
{
    public class NhomVatTuHangHoa : DoiTuong
    {
        #region Ma property
        public string Ma
        {
            get
            {
                return (string)_DataRow["Ma"];
            }
            set
            {
                _DataRow["Ma"] = value;
            }
        }
        #endregion

        #region Ten property
        public string Ten
        {
            get
            {
                return (string)_DataRow["Ten"];
            }
            set
            {
                _DataRow["Ten"] = value;
            }
        }
        #endregion

        #region TaiKhoanKho property
        public TaiKhoan TaiKhoanKho
        {
            get
            {
                if (_DataRow["TaiKhoanKhoID"] is Guid)
                    return DanhMucTaiKhoan.Instance.GetByProperty("ID", _DataRow["TaiKhoanKhoID"]);
                else
                    return null;
            }
            set
            {
                if (value is TaiKhoan)
                    _DataRow["TaiKhoanKhoID"] = value.ID;
                else
                    _DataRow["TaiKhoanKhoID"] = DBNull.Value;
            }
        }
        #endregion

        #region TaiKhoanGiaVon property
        public TaiKhoan TaiKhoanGiaVon
        {
            get
            {
                if (_DataRow["TaiKhoanGiaVonID"] is Guid)
                    return DanhMucTaiKhoan.Instance.GetByProperty("ID", _DataRow["TaiKhoanGiaVonID"]);
                else
                    return null;
            }
            set
            {
                if (value is TaiKhoan)
                    _DataRow["TaiKhoanGiaVonID"] = value.ID;
                else
                    _DataRow["TaiKhoanGiaVonID"] = DBNull.Value;
            }
        }
        #endregion

        #region NhomVatTuHangHoaCha property
        public NhomVatTuHangHoa NhomVatTuHangHoaCha
        {
            get
            {
                if ((_DataRow["NhomVatTuHangHoaChaID"] is Guid) && ((Guid)_DataRow["NhomVatTuHangHoaChaID"] != Guid.Empty))
                    return DanhMucNhomVatTuHangHoa.Instance.GetByID((Guid)_DataRow["NhomVatTuHangHoaChaID"]);
                else
                    return null;
            }
            set
            {
                if (value != null)
                    _DataRow["NhomVatTuHangHoaChaID"] = value.ID;
                else
                    _DataRow["NhomVatTuHangHoaChaID"] = DBNull.Value;
            }
        }
        #endregion

        #region LienQuanDenKho property
        public bool LienQuanDenKho
        {
            get
            {
                return (bool)_DataRow["LienQuanDenKho"];
            }
            set
            {
                _DataRow["LienQuanDenKho"] = value;
            }
        }
        #endregion

        #region PhuongPhapTinhGiaTonKho property
        public PhuongPhapTinhGiaTonKho PhuongPhapTinhGiaTonKho
        {
            get
            {
                if (_DataRow["PhuongPhapTinhGiaTonKhoID"] is Guid)
                    return DanhMucPhuongPhapTinhGiaTonKho.Instance.GetByProperty("ID", _DataRow["PhuongPhapTinhGiaTonKhoID"]);
                else
                    return null;
            }
            set
            {
                if (value is PhuongPhapTinhGiaTonKho)
                    _DataRow["PhuongPhapTinhGiaTonKhoID"] = value.ID;
                else
                    _DataRow["PhuongPhapTinhGiaTonKhoID"] = DBNull.Value;
            }
        }
        #endregion

        #region ThueSuat property
        public ThueSuat ThueSuat
        {
            get
            {
                if (_DataRow["ThueSuatID"] is Guid)
                    return DanhMucThueSuat.Instance.GetByProperty("ThueSuatID", _DataRow["ThueSuatID"]);
                else
                    return null;
            }
            set
            {
                if (value is ThueSuat)
                    _DataRow["ThueSuatID"] = value.ID;
                else
                    _DataRow["ThueSuatID"] = DBNull.Value;
            }
        }
        #endregion

        #region TaiKhoanVatVao property
        public TaiKhoan TaiKhoanVatVao
        {
            get
            {
                if (_DataRow["TaiKhoanVatVaoID"] is Guid)
                    return DanhMucTaiKhoan.Instance.GetByProperty("ID", _DataRow["TaiKhoanVatVaoID"]);
                else
                    return null;
            }
            set
            {
                if (value is TaiKhoan)
                    _DataRow["TaiKhoanVatVaoID"] = value.ID;
                else
                    _DataRow["TaiKhoanVatVaoID"] = DBNull.Value;
            }
        }
        #endregion

        #region TaiKhoanVatRa property
        public TaiKhoan TaiKhoanVatRa
        {
            get
            {
                if (_DataRow["TaiKhoanVatRaID"] is Guid)
                    return DanhMucTaiKhoan.Instance.GetByProperty("ID", _DataRow["TaiKhoanVatRaID"]);
                else
                    return null;
            }
            set
            {
                if (value is TaiKhoan)
                    _DataRow["TaiKhoanVatRaID"] = value.ID;
                else
                    _DataRow["TaiKhoanVatRaID"] = DBNull.Value;
            }
        }
        #endregion

        #region TaiKhoanDoanhThu property
        public TaiKhoan TaiKhoanDoanhThu
        {
            get
            {
                if (_DataRow["TaiKhoanDoanhThuID"] is Guid)
                    return DanhMucTaiKhoan.Instance.GetByProperty("ID", _DataRow["TaiKhoanDoanhThuID"]);
                else
                    return null;
            }
            set
            {
                if (value is TaiKhoan)
                    _DataRow["TaiKhoanDoanhThuID"] = value.ID;
                else
                    _DataRow["TaiKhoanDoanhThuID"] = DBNull.Value;
            }
        }
        #endregion

        #region IsSach property
        public bool IsSach
        {
            get
            {
                if (this.Ma == "SACH" )
                    return true;
                else if (this.NhomVatTuHangHoaCha == null) 
                    return false;
                else 
                    return this.NhomVatTuHangHoaCha.IsSach;
            }
        }
        #endregion   

        public NhomVatTuHangHoa(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucNhomVatTuHangHoa : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucNhomVatTuHangHoa _Instance;
        public static DanhMucNhomVatTuHangHoa Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucNhomVatTuHangHoa();
                    UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["Ma"]);
                    _Instance.DataTable.Constraints.Add(uniqueConstraint);
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    _Instance.DataView.Sort = "Ma";
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "NhomVatTuHangHoa";
            }
        }
        #endregion 

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new NhomVatTuHangHoa((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public NhomVatTuHangHoa this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new NhomVatTuHangHoa(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public NhomVatTuHangHoa NewDoiTuong()
        {
            return new NhomVatTuHangHoa(DataTable.NewRow());
        }

        public static DanhMucNhomVatTuHangHoa Filter(string filterExpression)
        {
            DanhMucNhomVatTuHangHoa obj = new DanhMucNhomVatTuHangHoa();
            obj.DataView = new DataView(DanhMucNhomVatTuHangHoa.Instance.DataTable, filterExpression, "Ma", DataViewRowState.CurrentRows);
            return obj;
        }


        public NhomVatTuHangHoa GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new NhomVatTuHangHoa(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public NhomVatTuHangHoa GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucNhomVatTuHangHoa GetAll()
        {
            return Instance;
        }
    }
}
